
--EXEC SP_AGED_BACKLOG_REPORT_BY_PRODUCT_PROC '0','0','0','0','WITHHOLD','REQUESTED','0','DETAIL'

ALTER PROC SP_AGED_BACKLOG_REPORT_BY_PRODUCT_PROC

@BASEPRODUCTID	VARCHAR(10),	-- Base Product Id
@MFGPRODUCTID	VARCHAR(10),	-- Child Product Id
@CUSTOMERTYPE	VARCHAR(20),	-- Customer Type
@CUSTOMERID	VARCHAR(10),	-- Customer Id or End Customer Id
@HOLDTYPE	VARCHAR(20),	-- Hold Type
@CATEGORY	VARCHAR(10),	-- Requested Date or Schedule Date  
@DEFAULT_PROMPT VARCHAR(20),
@GROUPTYPE	VARCHAR(20)	-- Detail OR Summary		
AS
BEGIN
	DECLARE @PRODUCT_ID INT
	DECLARE @PROCESSSTEP_ID INT
	DECLARE @PART_NUMBER VARCHAR(50)
	DECLARE @PRODUCT_TYPE_ID INT

	DECLARE @LINEITEM_ID INT
	DECLARE @COOL_QTY INT
	DECLARE @COOL_AMOUNT FLOAT
	DECLARE @COOL_REQUESTED_DATE DATETIME
	DECLARE @COOL_SCHEDULE_DATE DATETIME
	DECLARE @SO_BOOKING_DATE DATETIME
	DECLARE @PARTIAL_SHIPPED_QTY INT

	DECLARE @CATEGORY_DATE DATETIME

	DECLARE @BASEPRODUCT_ID INT
	DECLARE @BASEPRODUCT VARCHAR(50)

	DECLARE @SYSTEM_WEEK INT

	DECLARE @LINEITEM_WEEK INT
	DECLARE @LINEITEM_QTR  INT
	DECLARE @LINEITEM_YEAR  INT

	DECLARE @CURRENT_QTR  INT
	DECLARE @CURRENT_YEAR INT
	DECLARE @CURRENT_QTR_START DATETIME

	DECLARE @BOOKING_DATE_WEEK INT
	DECLARE @BOOKING_DATE_QTR INT
	DECLARE @BOOKING_DATE_YEAR INT

	DECLARE @DELINQUENT_QTY INT
	DECLARE @DELINQUENT_AMOUNT FLOAT

	DECLARE @BOOKINGWEEK VARCHAR(50)
	DECLARE @SYSTEM_DATE DATETIME
	DECLARE @SYSTEM_DATE_END_TIME DATETIME
	DECLARE @SYSTEM_DATE_STR VARCHAR(50)


	SET @CURRENT_QTR 	= DATEPART ( QQ, CURRENT_TIMESTAMP ) 
	SET @CURRENT_YEAR 	= DATEPART ( YYYY, CURRENT_TIMESTAMP )

	SET @SYSTEM_DATE_STR 	= CONVERT( VARCHAR, GETDATE(), 101 )+ ' 12:00AM'
	SET @SYSTEM_DATE 	= CONVERT( DATETIME, @SYSTEM_DATE_STR )
	SET @SYSTEM_DATE_STR	= NULL
	SET @SYSTEM_DATE_STR 		= CONVERT( VARCHAR, GETDATE(), 101 )+ ' 11:59PM'
	SET @SYSTEM_DATE_END_TIME 	= CONVERT( DATETIME, @SYSTEM_DATE_STR )
	

	DECLARE @RESULTS_TABLE TABLE ( 
				       BASEPRODUCT_ID INT,	
				       BASEPRODUCT VARCHAR(50),
				       PRODUCT_ID INT,	
 				       PART_NUMBER VARCHAR(50),
				       BOOKING_WEEK VARCHAR(10),
				       DELIQUENT_QTY INT,
				       DELIQUENT_AMOUNT FLOAT,
				       WEEK1_QTY INT,
				       WEEK1_AMOUNT FLOAT,
				       WEEK2_QTY INT,
				       WEEK2_AMOUNT FLOAT,
				       WEEK3_QTY INT,
				       WEEK3_AMOUNT FLOAT,
				       WEEK4_QTY INT,
				       WEEK4_AMOUNT FLOAT,
				       WEEK5_QTY INT,
				       WEEK5_AMOUNT FLOAT,
				       WEEK6_QTY INT,
				       WEEK6_AMOUNT FLOAT,
				       WEEK7_QTY INT,
				       WEEK7_AMOUNT FLOAT,
				       WEEK8_QTY INT,
				       WEEK8_AMOUNT FLOAT,
				       WEEK9_QTY INT,
				       WEEK9_AMOUNT FLOAT,
				       WEEK10_QTY INT,
				       WEEK10_AMOUNT FLOAT,
				       WEEK11_QTY INT,
				       WEEK11_AMOUNT FLOAT,
				       WEEK12_QTY INT,
				       WEEK12_AMOUNT FLOAT,
				       WEEK13_QTY INT,
				       WEEK13_AMOUNT FLOAT,												
				       CQTRPLUS1_QTY INT,
				       CQTRPLUS1_AMOUNT FLOAT, 	
				       CQTRPLUS2_QTY INT,
				       CQTRPLUS2_AMOUNT FLOAT, 			 		
				       CQTRPLUS3_QTY INT,
				       CQTRPLUS3_AMOUNT FLOAT, 			 				 		
				       PRODUCT_TYPE_ID INT,
				       WEEK_ID INT,
				       TOTAL_WEEKS_QTY INT,
				       TOTAL_WEEKS_AMOUNT FLOAT)	

	DECLARE @PRODUCT_CURSOR CURSOR
	DECLARE @LINE_ITEM_CURSOR CURSOR

	IF @BASEPRODUCTID = '0' 
	BEGIN
		SET @PRODUCT_CURSOR = CURSOR FOR
		SELECT PRODUCT_ID,PRO.PROCESSSTEP_ID,PART_NUMBER, 
			CASE 
				WHEN PRO.P_TYPE IS NULL THEN 1
				WHEN PRO.P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 2
				WHEN PRO.P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 3
			END AS PRODUCT_TYPE_ID 
		FROM PRODUCT PRO , PROCESS_STEP PS
		WHERE PRO.PROCESSSTEP_ID = PS.PROCESSSTEP_ID AND PS_TYPE_NAME = 'FINISHED GOODS' --AND PRO.PRODUCT_ID = 1044
	END
	ELSE
	BEGIN
		SET @PRODUCT_CURSOR = CURSOR FOR
		SELECT PRODUCT_ID, PRO.PROCESSSTEP_ID, PART_NUMBER, 
			CASE 
				WHEN PRO.P_TYPE IS NULL THEN 1
				WHEN PRO.P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 2
				WHEN PRO.P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 3
			END AS PRODUCT_TYPE_ID 
		FROM PRODUCT PRO , PROCESS_STEP PS
		WHERE PRO.PROCESSSTEP_ID = PS.PROCESSSTEP_ID AND PS_TYPE_NAME = 'FINISHED GOODS' 
		AND ( PRO.P_PARENT_PRODUCT_ID = @BASEPRODUCTID OR PRO.PRODUCT_ID = @BASEPRODUCTID )
		AND PRO.P_PARENT_PRODUCT_ID IS NOT NULL 	
	END

	OPEN @PRODUCT_CURSOR
	FETCH NEXT FROM @PRODUCT_CURSOR INTO @PRODUCT_ID, @PROCESSSTEP_ID, @PART_NUMBER, @PRODUCT_TYPE_ID

	WHILE @@FETCH_STATUS = 0
	BEGIN
		SELECT @BASEPRODUCT_ID =
			CASE 
				WHEN P_PARENT_PRODUCT_ID = -1 THEN PRODUCT_ID 
				WHEN P_PARENT_PRODUCT_ID <> -1 THEN P_PARENT_PRODUCT_ID	
			END 
		FROM PRODUCT WHERE PRODUCT_ID = @PRODUCT_ID

		SELECT @BASEPRODUCT = PART_NUMBER FROM PRODUCT WHERE PRODUCT_ID = @BASEPRODUCT_ID

		--PRINT ' @CURRENT_QTR = ' + CONVERT(VARCHAR, @CURRENT_QTR) + ' @BASEPRODUCT_ID = ' + CONVERT(VARCHAR,@BASEPRODUCT_ID) + ' @BASEPRODUCT = ' + @BASEPRODUCT + ' @PRODUCT_ID = ' + CONVERT(VARCHAR,@PRODUCT_ID)  

		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'CURRENT', @PRODUCT_TYPE_ID, 0 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK1', @PRODUCT_TYPE_ID, 1 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK2', @PRODUCT_TYPE_ID, 2 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK3', @PRODUCT_TYPE_ID, 3 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK4', @PRODUCT_TYPE_ID, 4 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK5', @PRODUCT_TYPE_ID, 5 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK6', @PRODUCT_TYPE_ID, 6 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK7', @PRODUCT_TYPE_ID, 7 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK8', @PRODUCT_TYPE_ID, 8 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK9', @PRODUCT_TYPE_ID, 9 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK10', @PRODUCT_TYPE_ID, 10 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK11', @PRODUCT_TYPE_ID, 11 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK12', @PRODUCT_TYPE_ID, 12 )
		INSERT INTO @RESULTS_TABLE (BASEPRODUCT_ID, BASEPRODUCT, PRODUCT_ID, PART_NUMBER, BOOKING_WEEK, PRODUCT_TYPE_ID, WEEK_ID ) VALUES ( @BASEPRODUCT_ID , @BASEPRODUCT, @PRODUCT_ID, @PART_NUMBER, 'WEEK13', @PRODUCT_TYPE_ID, 13 )

		
		IF ( @CURRENT_QTR = 1 ) 
		BEGIN
			SET @SYSTEM_WEEK = 1	
			SET @CURRENT_QTR_START = '01/01/' + CONVERT(VARCHAR,@CURRENT_YEAR)
			
			/*SELECT @DELINQUENT_QTY = SUM(COOL_QTY), @DELINQUENT_AMOUNT = SUM(COOL_AMOUNT)
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL, SALES_ORDER SO 
			WHERE PRODUCT_ID = @PRODUCT_ID AND COOL.SALESORDER_ID = SO.SALESORDER_ID
			AND SO.SO_IS_SAMPLE = 1 AND COOL_REQUESTED_DATE < '01/01/' + CONVERT(VARCHAR,@CURRENT_YEAR) AND COOL_STATUS <> 'SHIPPED'*/
		END
		IF ( @CURRENT_QTR = 2 )
		BEGIN
			SET @SYSTEM_WEEK = 14		
			SET @CURRENT_QTR_START = '04/01/' + CONVERT(VARCHAR,@CURRENT_YEAR)

			/*SELECT @DELINQUENT_QTY = SUM(COOL_QTY), @DELINQUENT_AMOUNT = SUM(COOL_AMOUNT)
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL, SALES_ORDER SO 
			WHERE PRODUCT_ID = @PRODUCT_ID AND COOL.SALESORDER_ID = SO.SALESORDER_ID
			AND SO.SO_IS_SAMPLE = 1 AND COOL_REQUESTED_DATE < '04/01/' + CONVERT(VARCHAR,@CURRENT_YEAR) AND COOL_STATUS <> 'SHIPPED'*/
		END 
		IF ( @CURRENT_QTR = 3 ) 
		BEGIN
			SET @SYSTEM_WEEK = 27
			SET @CURRENT_QTR_START = '07/01/' + CONVERT(VARCHAR,@CURRENT_YEAR)
			/*SELECT @DELINQUENT_QTY = SUM(COOL_QTY), @DELINQUENT_AMOUNT = SUM(COOL_AMOUNT)
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL, SALES_ORDER SO 
			WHERE PRODUCT_ID = @PRODUCT_ID AND COOL.SALESORDER_ID = SO.SALESORDER_ID
			AND SO.SO_IS_SAMPLE = 1 AND COOL_REQUESTED_DATE < '07/01/' + CONVERT(VARCHAR,@CURRENT_YEAR) AND COOL_STATUS <> 'SHIPPED'*/
		END
		IF ( @CURRENT_QTR = 4 ) 
		BEGIN
			SET @SYSTEM_WEEK = 40
			SET @CURRENT_QTR_START = '10/01/' + CONVERT(VARCHAR,@CURRENT_YEAR)

			/*SELECT @DELINQUENT_QTY = SUM(COOL_QTY), @DELINQUENT_AMOUNT = SUM(COOL_AMOUNT)
			FROM CUSTOMER_OPEN_ORDER_LINEITEM  COOL, SALES_ORDER SO 
			WHERE PRODUCT_ID = @PRODUCT_ID AND COOL.SALESORDER_ID = SO.SALESORDER_ID
			AND SO.SO_IS_SAMPLE = 1 AND COOL_REQUESTED_DATE < '10/01/' + CONVERT(VARCHAR,@CURRENT_YEAR) AND COOL_STATUS <> 'SHIPPED'*/
		END

		/*UPDATE @RESULTS_TABLE
		SET DELIQUENT_QTY = @DELINQUENT_QTY , DELIQUENT_AMOUNT = @DELINQUENT_AMOUNT
		WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
		*/
		
		IF ( @HOLDTYPE = 'WITHHOLD' )
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR
			SELECT LINEITEM_ID, ISNULL( COOL_QTY, 0 ), COOL_UNIT_COST/*COOL_AMOUNT*/, COOL_REQUESTED_DATE, COOL_SCHEDULE_DATE , SO_BOOKING_DATE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL , SALES_ORDER SO 
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND PRODUCT_ID = @PRODUCT_ID AND COOL_STATUS <> 'SHIPPED' 

		END
		ELSE
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR
			SELECT LINEITEM_ID, ISNULL( COOL_QTY, 0 ), COOL_UNIT_COST/*COOL_AMOUNT*/, COOL_REQUESTED_DATE, COOL_SCHEDULE_DATE , SO_BOOKING_DATE
			FROM CUSTOMER_OPEN_ORDER_LINEITEM COOL , SALES_ORDER SO 
			WHERE SO.SO_IS_SAMPLE = 1 AND COOL.SALESORDER_ID = SO.SALESORDER_ID AND PRODUCT_ID = @PRODUCT_ID AND COOL_STATUS = 'APPROVED' 
		END


		OPEN @LINE_ITEM_CURSOR
		FETCH NEXT FROM @LINE_ITEM_CURSOR 
		INTO @LINEITEM_ID, @COOL_QTY, @COOL_AMOUNT , @COOL_REQUESTED_DATE, @COOL_SCHEDULE_DATE, @SO_BOOKING_DATE

		WHILE @@FETCH_STATUS = 0
		BEGIN
			IF ( @CATEGORY = 'REQUESTED' )
			BEGIN
				SET @CATEGORY_DATE = @COOL_REQUESTED_DATE
			END
			ELSE
			BEGIN
				SET @CATEGORY_DATE = ISNULL(@COOL_SCHEDULE_DATE , getDate() )
			END
		
			SET @LINEITEM_QTR  = DATEPART (QQ, @CATEGORY_DATE )
			SET @LINEITEM_YEAR = DATEPART (YYYY, @CATEGORY_DATE )

			SET @BOOKING_DATE_QTR = DATEPART (QQ, @SO_BOOKING_DATE )
			SET @BOOKING_DATE_YEAR = DATEPART (YYYY, @SO_BOOKING_DATE ) 

			SELECT @PARTIAL_SHIPPED_QTY = ISNULL( SUM( CSLIT_Qty_Shipped ), 0 ) FROM Customer_Shipping_Line_Item_Table WHERE LineItem_ID = @LINEITEM_ID and CSLIT_Status = 'SHIPPED'

			IF ( @COOL_QTY - @PARTIAL_SHIPPED_QTY > 0 )
			BEGIN
				SET @COOL_QTY = @COOL_QTY - @PARTIAL_SHIPPED_QTY
				
				IF ( @LINEITEM_QTR = @CURRENT_QTR AND @LINEITEM_YEAR = @CURRENT_YEAR AND @CATEGORY_DATE >= @SYSTEM_DATE )
				BEGIN
					SET @LINEITEM_WEEK = DATEPART( WW, @CATEGORY_DATE )
					SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )

					--IF ( @BOOKING_DATE_QTR < @CURRENT_QTR )
					IF (  @SO_BOOKING_DATE < @CURRENT_QTR_START )
					BEGIN
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK ) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK1_QTY = ISNULL(WEEK1_QTY,0) + @COOL_QTY , WEEK1_AMOUNT = ISNULL(WEEK1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 1) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK2_QTY = ISNULL(WEEK2_QTY,0) + @COOL_QTY , WEEK2_AMOUNT = ISNULL(WEEK2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 2) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK3_QTY = ISNULL(WEEK3_QTY,0) + @COOL_QTY , WEEK3_AMOUNT = ISNULL(WEEK3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'	
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 3) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK4_QTY = ISNULL(WEEK4_QTY,0) + @COOL_QTY , WEEK4_AMOUNT = ISNULL(WEEK4_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 4) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK5_QTY = ISNULL(WEEK5_QTY,0) + @COOL_QTY , WEEK5_AMOUNT = ISNULL(WEEK5_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 5) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK6_QTY = ISNULL(WEEK6_QTY,0) + @COOL_QTY , WEEK6_AMOUNT = ISNULL(WEEK6_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 6) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK7_QTY = ISNULL(WEEK7_QTY,0) + @COOL_QTY , WEEK7_AMOUNT = ISNULL(WEEK7_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 7) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK8_QTY = ISNULL(WEEK8_QTY,0) + @COOL_QTY , WEEK8_AMOUNT = ISNULL(WEEK8_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 8) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK9_QTY = ISNULL(WEEK9_QTY,0) + @COOL_QTY , WEEK9_AMOUNT = ISNULL(WEEK9_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 9) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK10_QTY = ISNULL(WEEK10_QTY,0) + @COOL_QTY , WEEK10_AMOUNT = ISNULL(WEEK10_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 10) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK11_QTY = ISNULL(WEEK11_QTY,0) + @COOL_QTY , WEEK11_AMOUNT = ISNULL(WEEK11_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 11) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK12_QTY = ISNULL(WEEK12_QTY,0) + @COOL_QTY , WEEK12_AMOUNT = ISNULL(WEEK12_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 12) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK13_QTY = ISNULL(WEEK13_QTY,0) + @COOL_QTY , WEEK13_AMOUNT = ISNULL(WEEK13_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
						END
					END
					ELSE IF ( @BOOKING_DATE_QTR = @CURRENT_QTR )
					BEGIN

						SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, ( @BOOKING_DATE_WEEK - ( @CURRENT_QTR - 1 )* 13 )  )
						--PRINT 'BOOKING WEEK ' + @BOOKINGWEEK
						IF( @SO_BOOKING_DATE < @SYSTEM_DATE )
						BEGIN
							SET @BOOKINGWEEK = 'CURRENT'
						END
						--PRINT ' @SO_BOOKING_DATE =  ' + CONVERT(VARCHAR,@SO_BOOKING_DATE) + ' @SYSTEM_DATE =  ' + CONVERT(VARCHAR,@SYSTEM_DATE) + ' GETDATE() =  ' + CONVERT(VARCHAR,GETDATE())  	

						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK ) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK1_QTY = ISNULL(WEEK1_QTY,0) + @COOL_QTY , WEEK1_AMOUNT = ISNULL(WEEK1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK  = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 1) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK2_QTY = ISNULL(WEEK2_QTY,0) + @COOL_QTY , WEEK2_AMOUNT = ISNULL(WEEK2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 2) 
						BEGIN
							
							UPDATE @RESULTS_TABLE
							SET WEEK3_QTY = ISNULL(WEEK3_QTY,0) + @COOL_QTY , WEEK3_AMOUNT = ISNULL(WEEK3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 3) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK4_QTY = ISNULL(WEEK4_QTY,0) + @COOL_QTY , WEEK4_AMOUNT = ISNULL(WEEK4_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 4) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK5_QTY = ISNULL(WEEK5_QTY,0) + @COOL_QTY , WEEK5_AMOUNT = ISNULL(WEEK5_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 5) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK6_QTY = ISNULL(WEEK6_QTY,0) + @COOL_QTY , WEEK6_AMOUNT = ISNULL(WEEK6_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 6) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK7_QTY = ISNULL(WEEK7_QTY,0) + @COOL_QTY , WEEK7_AMOUNT = ISNULL(WEEK7_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 7) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK8_QTY = ISNULL(WEEK8_QTY,0) + @COOL_QTY , WEEK8_AMOUNT = ISNULL(WEEK8_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 8) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK9_QTY = ISNULL(WEEK9_QTY,0) + @COOL_QTY , WEEK9_AMOUNT = ISNULL(WEEK9_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 9) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK10_QTY = ISNULL(WEEK10_QTY,0) + @COOL_QTY , WEEK10_AMOUNT = ISNULL(WEEK10_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 10) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK11_QTY = ISNULL(WEEK11_QTY,0) + @COOL_QTY , WEEK11_AMOUNT = ISNULL(WEEK11_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 11) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK12_QTY = ISNULL(WEEK12_QTY,0) + @COOL_QTY , WEEK12_AMOUNT = ISNULL(WEEK12_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END
						IF ( @LINEITEM_WEEK = @SYSTEM_WEEK + 12) 
						BEGIN
							UPDATE @RESULTS_TABLE
							SET WEEK13_QTY = ISNULL(WEEK13_QTY,0) + @COOL_QTY , WEEK13_AMOUNT = ISNULL(WEEK13_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
							WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
						END 
					END
				END
				ELSE IF ( @LINEITEM_QTR = @CURRENT_QTR + 1 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )
					--SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, ( @BOOKING_DATE_WEEK - ( @LINEITEM_QTR - 2 )* 13 )  )
					SET @BOOKINGWEEK = 'WEEK' + CONVERT(VARCHAR , CASE 
								 	WHEN @BOOKING_DATE_WEEK > 13 AND @BOOKING_DATE_WEEK <= 26 THEN @BOOKING_DATE_WEEK - 13 
								 	WHEN @BOOKING_DATE_WEEK > 26 AND @BOOKING_DATE_WEEK <= 39 THEN @BOOKING_DATE_WEEK - 26 			
								 	WHEN @BOOKING_DATE_WEEK > 39 THEN @BOOKING_DATE_WEEK - 39 
								 	ELSE @BOOKING_DATE_WEEK END )
					IF( @SO_BOOKING_DATE < @SYSTEM_DATE )
					BEGIN
						SET @BOOKINGWEEK = 'CURRENT'
					END
					ELSE IF( @BOOKING_DATE_QTR > @CURRENT_QTR )
					BEGIN
						SET @BOOKINGWEEK = '-1'
					END

					UPDATE @RESULTS_TABLE
					SET CQTRPLUS1_QTY = ISNULL(CQTRPLUS1_QTY,0) + @COOL_QTY , CQTRPLUS1_AMOUNT = ISNULL(CQTRPLUS1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
				END
				ELSE IF ( @LINEITEM_QTR = @CURRENT_QTR + 2 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )
					--SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, ( @BOOKING_DATE_WEEK - ( @LINEITEM_QTR - 3 )* 13 )  )
					SET @BOOKINGWEEK = 'WEEK' + CONVERT(VARCHAR , CASE 
								 	WHEN @BOOKING_DATE_WEEK > 13 AND @BOOKING_DATE_WEEK <= 26 THEN @BOOKING_DATE_WEEK - 13 
								 	WHEN @BOOKING_DATE_WEEK > 26 AND @BOOKING_DATE_WEEK <= 39 THEN @BOOKING_DATE_WEEK - 26 			
								 	WHEN @BOOKING_DATE_WEEK > 39 THEN @BOOKING_DATE_WEEK - 39 
								 	ELSE @BOOKING_DATE_WEEK END )
					IF( @SO_BOOKING_DATE < @SYSTEM_DATE )
					BEGIN
						SET @BOOKINGWEEK = 'CURRENT'
					END
					ELSE IF( @BOOKING_DATE_QTR > @CURRENT_QTR )
					BEGIN
						SET @BOOKINGWEEK = '-1'
					END

					UPDATE @RESULTS_TABLE
					SET CQTRPLUS2_QTY = ISNULL(CQTRPLUS2_QTY,0) + @COOL_QTY , CQTRPLUS2_AMOUNT = ISNULL(CQTRPLUS2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
				END
				ELSE IF ( @LINEITEM_QTR = @CURRENT_QTR + 3 AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )
					--SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, ( @BOOKING_DATE_WEEK - ( @LINEITEM_QTR - 4 )* 13 )  )
					SET @BOOKINGWEEK = 'WEEK' + CONVERT(VARCHAR , CASE 
								 	WHEN @BOOKING_DATE_WEEK > 13 AND @BOOKING_DATE_WEEK <= 26 THEN @BOOKING_DATE_WEEK - 13 
								 	WHEN @BOOKING_DATE_WEEK > 26 AND @BOOKING_DATE_WEEK <= 39 THEN @BOOKING_DATE_WEEK - 26 			
								 	WHEN @BOOKING_DATE_WEEK > 39 THEN @BOOKING_DATE_WEEK - 39 
								 	ELSE @BOOKING_DATE_WEEK END )
					IF( @SO_BOOKING_DATE < @SYSTEM_DATE )
					BEGIN
						SET @BOOKINGWEEK = 'CURRENT'
					END
					ELSE IF( @BOOKING_DATE_QTR > @CURRENT_QTR )
					BEGIN
						SET @BOOKINGWEEK = '-1'
					END
					
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS3_QTY = ISNULL(CQTRPLUS3_QTY,0) + @COOL_QTY , CQTRPLUS3_AMOUNT = ISNULL(CQTRPLUS3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
				END
				ELSE IF ( ( ( @LINEITEM_QTR + 5 ) - @CURRENT_QTR ) = 2 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
					SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )
					/*IF @BOOKING_DATE_WEEK > 13
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 13
					END
					ELSE IF @BOOKING_DATE_WEEK > 26
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 26
					END
					ELSE IF @BOOKING_DATE_WEEK > 39
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 39
					END

					SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, @BOOKING_DATE_WEEK )*/
					SET @BOOKINGWEEK = 'WEEK' + CONVERT(VARCHAR , CASE 
								 	WHEN @BOOKING_DATE_WEEK > 13 AND @BOOKING_DATE_WEEK <= 26 THEN @BOOKING_DATE_WEEK - 13 
								 	WHEN @BOOKING_DATE_WEEK > 26 AND @BOOKING_DATE_WEEK <= 39 THEN @BOOKING_DATE_WEEK - 26 			
								 	WHEN @BOOKING_DATE_WEEK > 39 THEN @BOOKING_DATE_WEEK - 39 
								 	ELSE @BOOKING_DATE_WEEK END )
					IF( @SO_BOOKING_DATE < @SYSTEM_DATE )
					BEGIN
						SET @BOOKINGWEEK = 'CURRENT'
					END
					ELSE IF( @BOOKING_DATE_QTR > @CURRENT_QTR )
					BEGIN
						SET @BOOKINGWEEK = '-1'
					END	

					UPDATE @RESULTS_TABLE
					SET CQTRPLUS1_QTY = ISNULL(CQTRPLUS1_QTY,0) + @COOL_QTY , CQTRPLUS1_AMOUNT = ISNULL(CQTRPLUS1_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
					
				END
				ELSE IF ( ( @LINEITEM_QTR + 5 - @CURRENT_QTR ) = 3 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
					SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )

					/*IF @BOOKING_DATE_WEEK > 13
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 13
					END
					ELSE IF @BOOKING_DATE_WEEK > 26
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 26
					END
					ELSE IF @BOOKING_DATE_WEEK > 39
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 39
					END

					SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, @BOOKING_DATE_WEEK )*/
					SET @BOOKINGWEEK = 'WEEK' + CONVERT(VARCHAR , CASE 
								 	WHEN @BOOKING_DATE_WEEK > 13 AND @BOOKING_DATE_WEEK <= 26 THEN @BOOKING_DATE_WEEK - 13 
								 	WHEN @BOOKING_DATE_WEEK > 26 AND @BOOKING_DATE_WEEK <= 39 THEN @BOOKING_DATE_WEEK - 26 			
								 	WHEN @BOOKING_DATE_WEEK > 39 THEN @BOOKING_DATE_WEEK - 39 
								 	ELSE @BOOKING_DATE_WEEK END )
					IF( @SO_BOOKING_DATE < @SYSTEM_DATE )
					BEGIN
						SET @BOOKINGWEEK = 'CURRENT'
					END
					ELSE IF( @BOOKING_DATE_QTR > @CURRENT_QTR )
					BEGIN
						SET @BOOKINGWEEK = '-1'
					END	

					UPDATE @RESULTS_TABLE
					SET CQTRPLUS2_QTY = ISNULL(CQTRPLUS2_QTY,0) + @COOL_QTY , CQTRPLUS2_AMOUNT = ISNULL(CQTRPLUS2_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
				END
				ELSE IF ( ( @LINEITEM_QTR + 5 - @CURRENT_QTR ) = 4 AND @LINEITEM_YEAR = @CURRENT_YEAR + 1 )
				BEGIN
					SET @BOOKING_DATE_WEEK = DATEPART( WW, @SO_BOOKING_DATE )

					/*IF @BOOKING_DATE_WEEK > 13
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 13
					END
					ELSE IF @BOOKING_DATE_WEEK > 26
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 26
					END
					ELSE IF @BOOKING_DATE_WEEK > 39
					BEGIN
						SET @BOOKING_DATE_WEEK = @BOOKING_DATE_WEEK - 39
					END

					SET @BOOKINGWEEK = 'WEEK' + CONVERT( VARCHAR, @BOOKING_DATE_WEEK )*/
					SET @BOOKINGWEEK = 'WEEK' + CONVERT(VARCHAR , CASE 
								 	WHEN @BOOKING_DATE_WEEK > 13 AND @BOOKING_DATE_WEEK <= 26 THEN @BOOKING_DATE_WEEK - 13 
								 	WHEN @BOOKING_DATE_WEEK > 26 AND @BOOKING_DATE_WEEK <= 39 THEN @BOOKING_DATE_WEEK - 26 			
								 	WHEN @BOOKING_DATE_WEEK > 39 THEN @BOOKING_DATE_WEEK - 39 
								 	ELSE @BOOKING_DATE_WEEK END )
					IF( @SO_BOOKING_DATE < @SYSTEM_DATE )
					BEGIN
						SET @BOOKINGWEEK = 'CURRENT'
					END
					ELSE IF( @BOOKING_DATE_QTR > @CURRENT_QTR )
					BEGIN
						SET @BOOKINGWEEK = '-1'
					END
	
					UPDATE @RESULTS_TABLE
					SET CQTRPLUS3_QTY = ISNULL(CQTRPLUS3_QTY,0) + @COOL_QTY , CQTRPLUS3_AMOUNT = ISNULL(CQTRPLUS3_AMOUNT,0) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = @BOOKINGWEEK
				END
				ELSE IF ( @LINEITEM_QTR = @CURRENT_QTR AND @LINEITEM_YEAR = @CURRENT_YEAR AND @CATEGORY_DATE < @SYSTEM_DATE )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET DELIQUENT_QTY = ISNULL( DELIQUENT_QTY, 0 ) + @COOL_QTY, DELIQUENT_AMOUNT = ISNULL( DELIQUENT_AMOUNT, 0 ) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
				END	
				ELSE IF ( @LINEITEM_QTR < @CURRENT_QTR AND @LINEITEM_YEAR = @CURRENT_YEAR )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET DELIQUENT_QTY = ISNULL( DELIQUENT_QTY, 0 ) + @COOL_QTY, DELIQUENT_AMOUNT = ISNULL( DELIQUENT_AMOUNT, 0 ) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
				END
				ELSE IF( @LINEITEM_YEAR < @CURRENT_YEAR )
				BEGIN
					UPDATE @RESULTS_TABLE
					SET DELIQUENT_QTY = ISNULL( DELIQUENT_QTY, 0 ) + @COOL_QTY, DELIQUENT_AMOUNT = ISNULL( DELIQUENT_AMOUNT, 0 ) + ( @COOL_QTY * @COOL_AMOUNT )
					WHERE PRODUCT_ID = @PRODUCT_ID AND PART_NUMBER = @PART_NUMBER AND BOOKING_WEEK = 'CURRENT'
				END
			END			
			
			SET @COOL_QTY = NULL
			SET @COOL_AMOUNT = NULL
			SET @COOL_REQUESTED_DATE = NULL
			SET @COOL_SCHEDULE_DATE	= NULL

			FETCH NEXT FROM @LINE_ITEM_CURSOR 
			INTO @LINEITEM_ID, @COOL_QTY, @COOL_AMOUNT, @COOL_REQUESTED_DATE, @COOL_SCHEDULE_DATE, @SO_BOOKING_DATE 
		END
		CLOSE @LINE_ITEM_CURSOR
		DEALLOCATE @LINE_ITEM_CURSOR

		SET @PRODUCT_ID = NULL
		SET @PROCESSSTEP_ID = NULL
		SET @PART_NUMBER = NULL

		FETCH NEXT FROM @PRODUCT_CURSOR INTO @PRODUCT_ID, @PROCESSSTEP_ID, @PART_NUMBER, @PRODUCT_TYPE_ID
	END
	CLOSE @PRODUCT_CURSOR
 	DEALLOCATE @PRODUCT_CURSOR
	
	DECLARE @CUMULATIVE_QTY INT 	
	DECLARE @BOOKING_WEEK_ID INT
	DECLARE @TOTAL_WEEKS_QTY INT
	DECLARE @TOTAL_WEEKS_AMOUNT FLOAT
	DECLARE @TEMP_TOTAL_WEEKS_QTY INT
	DECLARE @TEMP_TOTAL_WEEKS_AMOUNT FLOAT	
	DECLARE @CQTRPLUS1_QTY INT
	DECLARE @CQTRPLUS1_AMOUNT FLOAT
	DECLARE @CQTRPLUS2_QTY INT
	DECLARE @CQTRPLUS2_AMOUNT FLOAT
	DECLARE @CQTRPLUS3_QTY INT
	DECLARE @CQTRPLUS3_AMOUNT FLOAT

	SET @TEMP_TOTAL_WEEKS_QTY = 0
	SET @TEMP_TOTAL_WEEKS_AMOUNT = 0	
	SET @TOTAL_WEEKS_QTY = 0
	SET @TOTAL_WEEKS_AMOUNT = 0	
	SET @CQTRPLUS1_QTY = 0
	SET @CQTRPLUS1_AMOUNT = 0
	SET @CQTRPLUS2_QTY = 0
	SET @CQTRPLUS2_AMOUNT = 0
	SET @CQTRPLUS3_QTY = 0
	SET @CQTRPLUS3_AMOUNT = 0
	SET @CUMULATIVE_QTY = 0

	SET @PRODUCT_CURSOR = CURSOR FOR
	SELECT BASEPRODUCT_ID, PRODUCT_ID, WEEK_ID FROM @RESULTS_TABLE GROUP BY BASEPRODUCT_ID, PRODUCT_ID, WEEK_ID 

	OPEN @PRODUCT_CURSOR
	FETCH NEXT FROM @PRODUCT_CURSOR INTO @BASEPRODUCT_ID, @PRODUCT_ID, @BOOKING_WEEK_ID

	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @LINE_ITEM_CURSOR = CURSOR FOR SELECT TOTAL_WEEKS_QTY, TOTAL_WEEKS_AMOUNT, CQTRPLUS1_QTY, CQTRPLUS1_AMOUNT, CQTRPLUS2_QTY, CQTRPLUS2_AMOUNT, CQTRPLUS3_QTY, CQTRPLUS3_AMOUNT
		FROM @RESULTS_TABLE WHERE PRODUCT_ID = @PRODUCT_ID AND WEEK_ID = @BOOKING_WEEK_ID

		OPEN @LINE_ITEM_CURSOR 
		FETCH NEXT FROM @LINE_ITEM_CURSOR  INTO @TOTAL_WEEKS_QTY, @TOTAL_WEEKS_AMOUNT, @CQTRPLUS1_QTY, @CQTRPLUS1_AMOUNT, @CQTRPLUS2_QTY, @CQTRPLUS2_AMOUNT, @CQTRPLUS3_QTY, @CQTRPLUS3_AMOUNT  
		WHILE @@FETCH_STATUS = 0
		BEGIN	
			UPDATE @RESULTS_TABLE SET 
				TOTAL_WEEKS_QTY	= ISNULL(TOTAL_WEEKS_QTY,0) + ISNULL(DELIQUENT_QTY,0) + ISNULL(WEEK1_QTY,0) + ISNULL(WEEK2_QTY,0) + ISNULL(WEEK3_QTY,0) + ISNULL(WEEK4_QTY,0) + ISNULL(WEEK5_QTY,0) + ISNULL(WEEK6_QTY,0) + ISNULL(WEEK7_QTY,0) + ISNULL(WEEK8_QTY,0) + ISNULL(WEEK9_QTY,0) + ISNULL(WEEK10_QTY,0) + ISNULL(WEEK11_QTY,0) + ISNULL(WEEK12_QTY,0) + ISNULL(WEEK13_QTY,0),		
				TOTAL_WEEKS_AMOUNT = ISNULL(TOTAL_WEEKS_AMOUNT,0) + ISNULL(DELIQUENT_AMOUNT,0) + ISNULL(WEEK1_AMOUNT,0) + ISNULL(WEEK2_AMOUNT,0) + ISNULL(WEEK3_AMOUNT,0) + ISNULL(WEEK4_AMOUNT,0) + ISNULL(WEEK5_AMOUNT,0) + ISNULL(WEEK6_AMOUNT,0) + ISNULL(WEEK7_AMOUNT,0) + ISNULL(WEEK8_AMOUNT,0) + ISNULL(WEEK9_AMOUNT,0) + ISNULL(WEEK10_AMOUNT,0) + ISNULL(WEEK11_AMOUNT,0) + ISNULL(WEEK12_AMOUNT,0) + ISNULL(WEEK13_AMOUNT,0)
			WHERE PRODUCT_ID = @PRODUCT_ID AND WEEK_ID = @BOOKING_WEEK_ID

			SELECT 	@TEMP_TOTAL_WEEKS_QTY = TOTAL_WEEKS_QTY,
				@TEMP_TOTAL_WEEKS_AMOUNT = TOTAL_WEEKS_AMOUNT 
			FROM @RESULTS_TABLE WHERE PRODUCT_ID = @PRODUCT_ID AND WEEK_ID = @BOOKING_WEEK_ID
	
			UPDATE @RESULTS_TABLE SET 
				TOTAL_WEEKS_QTY 	= ISNULL(TOTAL_WEEKS_QTY,0) + ISNULL(@TEMP_TOTAL_WEEKS_QTY,0), 
				TOTAL_WEEKS_AMOUNT 	= ISNULL(TOTAL_WEEKS_AMOUNT,0) + ISNULL(@TEMP_TOTAL_WEEKS_AMOUNT,0),
				CQTRPLUS1_QTY 		= ISNULL( CQTRPLUS1_QTY, 0) + ISNULL( @CQTRPLUS1_QTY, 0),
				CQTRPLUS1_AMOUNT 	= ISNULL( CQTRPLUS1_AMOUNT, 0) + ISNULL( @CQTRPLUS1_AMOUNT, 0),
				CQTRPLUS2_QTY 		= ISNULL( CQTRPLUS2_QTY, 0) + ISNULL( @CQTRPLUS2_QTY, 0),
				CQTRPLUS2_AMOUNT 	= ISNULL( CQTRPLUS2_AMOUNT, 0) + ISNULL( @CQTRPLUS2_AMOUNT, 0),
				CQTRPLUS3_QTY 		= ISNULL( CQTRPLUS3_QTY, 0) + ISNULL( @CQTRPLUS3_QTY, 0),
				CQTRPLUS3_AMOUNT 	= ISNULL( CQTRPLUS3_AMOUNT, 0) + ISNULL( @CQTRPLUS3_AMOUNT, 0)
			WHERE PRODUCT_ID = @PRODUCT_ID AND WEEK_ID = (@BOOKING_WEEK_ID + 1)
			SET @TOTAL_WEEKS_QTY = 0
			SET @TOTAL_WEEKS_AMOUNT = 0	
			SET @TEMP_TOTAL_WEEKS_QTY = 0
			SET @TEMP_TOTAL_WEEKS_AMOUNT = 0
			SET @CQTRPLUS1_QTY = 0
			SET @CQTRPLUS1_AMOUNT = 0
			SET @CQTRPLUS2_QTY = 0
			SET @CQTRPLUS2_AMOUNT = 0
			SET @CQTRPLUS3_QTY = 0
			SET @CQTRPLUS3_AMOUNT = 0
			

			FETCH NEXT FROM @LINE_ITEM_CURSOR INTO @TOTAL_WEEKS_QTY, @TOTAL_WEEKS_AMOUNT, @CQTRPLUS1_QTY, @CQTRPLUS1_AMOUNT, @CQTRPLUS2_QTY, @CQTRPLUS2_AMOUNT, @CQTRPLUS3_QTY, @CQTRPLUS3_AMOUNT  
		END
		CLOSE @LINE_ITEM_CURSOR
		DEALLOCATE @LINE_ITEM_CURSOR

		SET @BASEPRODUCT_ID = NULL
		SET @PRODUCT_ID = NULL
		SET @BOOKING_WEEK_ID = 0
		
		FETCH NEXT FROM @PRODUCT_CURSOR INTO @BASEPRODUCT_ID, @PRODUCT_ID, @BOOKING_WEEK_ID
	END
	CLOSE @PRODUCT_CURSOR
 	DEALLOCATE @PRODUCT_CURSOR

	SELECT * FROM @RESULTS_TABLE  ORDER BY PRODUCT_TYPE_ID, PRODUCT_ID, PART_NUMBER, WEEK_ID
END